/* 
 * PostConvertor.cpp
 *  Created on: 2011-03-25
 *      Author: MillionLee
 */
#include "StdAfx.h"
#include "PostConvertor.h"
#include "imageGet.h"

#include "config.h"

PostConvertor::PostConvertor(void)
{
}

PostConvertor::~PostConvertor(void)
{
}

RePost PostConvertor::Quote2Re(const QuotePost &quotePost, bool &result)//!need Refactoring!
{
	result = false;

	//check post length
	wstring rePostText = quotePost.get_text() + genFromTencentSubFix(quotePost.get_postTime());
	if (rePostText.length() >= 140*2)
		rePostText = quotePost.get_text();

	//get image
	if (quotePost.get_imageUrl() != "")
	{
		string imageFileNamePath = imageGet()(quotePost.get_imageUrl(), quotePost.get_id());
		//generate post
		if (imageFileNamePath != "")
		{
			result = true;
			return RePost(rePostText, imageFileNamePath);//return a rePost with image.
		}
		else
		{
			return RePost(L"", "");//return illegal rePost and false;
		}
	}
	else
	{
		result = true;
		return RePost(rePostText, "");//return a rePost without image.
	}

}

wstring PostConvertor::formatTime(const time_t postTime)
{
	tm *postTM = localtime(&postTime);

	wchar_t *strPostTime = new wchar_t[UINT_FROM_TENCENT_TIME_LEN];
	wcsftime(strPostTime, UINT_FROM_TENCENT_TIME_LEN, STR_FROM_TENCENT_TIME_FORMAT.c_str(), postTM);
	wstring stringPostTime(strPostTime);
	delete[] strPostTime;

	return stringPostTime;
}

wstring PostConvertor::genFromTencentSubFix(const time_t postTime)
{
	return  wstring(L"(") + STR_FROM_TENCENT_PREFIX + formatTime(postTime) + STR_FROM_TENCENT_SUBFIX;
}